package ar.com.ford.it.fullEconomics.view.form;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;
import org.apache.struts.validator.ValidatorForm;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesr03Agreement;
import ar.com.ford.it.fullEconomics.util.FullEconomicsDateUtil;

public class AgreementsForm extends ValidatorForm {

    private String section;
    private String editBlocked;
    private String supplierCodeFind;
    private String supplierClassFind;
    private String supplierNameFind;
    private String commodityCode;
    private String partClass;
    private String prefix;
    private String base;
    private String suffix;
    private String dateFilter;
    private String dateFrom;
    private String dateTo;
    private String vehicleLine;
    private String supplierCode;
    private String supplierClass;
    private String description;
    private String econLevel;
    private String issueDate;
    private String effDate;
    private String signedDate;
    private String selectedIndex;
    private String userCode;
    private Teesr03Agreement agreementSelected;
    private Collection<Teesr03Agreement> agreementColl;

    public void resetForm() {
        this.section = "init";
        this.editBlocked = "false";
        this.supplierCodeFind = null;
        this.supplierClassFind = "-1";
        this.commodityCode = "-1";
        this.supplierNameFind = null;
        this.partClass = "-1";
        this.base = null;
        this.suffix = null;
        this.dateFrom = null;
        this.dateTo = null;
        this.supplierCode = null;
        this.supplierClass = "-1";
        this.econLevel = null;
        this.issueDate = null;
        this.effDate = null;
        this.signedDate = null;
        this.description = null;
        this.agreementSelected = null;
        this.agreementColl = null;
    }

    public void cleanCreateFields() {
        this.editBlocked = "false";
        this.supplierCode = null;
        this.supplierClass = "-1";
        this.econLevel = null;
        this.issueDate = null;
        this.effDate = null;
        this.description = null;
        this.signedDate = null;
    }

    public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest agreement) {

        ActionErrors ae = new ActionErrors();
        MessageResources resources = (MessageResources) agreement
                .getAttribute(org.apache.struts.Globals.MESSAGES_KEY);
        if ("find".equalsIgnoreCase(this.getSection())) {
            if (!StringUtils.isBlank(this.getDateFrom())) {
                if (StringUtils.isBlank(this.getDateTo())) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "agreement.error.invalidDateRange"));
                }
                try {
                    if (FullEconomicsDateUtil.format(this.getDateFrom()) == null) {
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("errors.date", this
                                        .getDateFrom()));
                    }
                } catch (Exception e) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "errors.date", this.getDateFrom()));
                }
            }
            if (!StringUtils.isBlank(this.getDateTo())) {
                if (StringUtils.isBlank(this.getDateFrom())) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "agreement.error.invalidDateRange"));
                }
                try {
                    if (FullEconomicsDateUtil.format(this.getDateTo()) == null) {
                        ae.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("errors.date", this
                                        .getDateTo()));
                    }
                } catch (Exception e) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "errors.date", this.getDateTo()));
                }
            }
        }
        if ("createUpdate".equalsIgnoreCase(this.getSection())) {

            // Verifico el codigo que no este vacio
            if (StringUtils.isBlank(this.getSupplierCode())) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "errors.required", resources
                                .getMessage("supplier.label.supplier")));
            }
            // Verifico y formateo la fechas
            if (StringUtils.isBlank(this.getEconLevel())) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "errors.required", resources
                                .getMessage("agreement.label.econLevel")));
            } else {
                if (FullEconomicsDateUtil.format(this.getEconLevel()) == null) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "errors.date", resources
                                    .getMessage("agreement.label.econLevel")));
                }
            }
            if (!StringUtils.isBlank(this.getEffDate())) {
                if (FullEconomicsDateUtil.format(this.getEffDate()) == null) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "errors.date", resources
                                    .getMessage("agreement.label.effDate")));
                }
            }
            if (!StringUtils.isBlank(this.getIssueDate())) {
                if (FullEconomicsDateUtil.format(this.getIssueDate()) == null) {
                    ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                            "errors.date", resources
                                    .getMessage("agreement.label.issuedDate")));
                }
            }
            if (!StringUtils.isBlank(this.getSignedDate())) {
                if (FullEconomicsDateUtil.format(this.getIssueDate()) == null) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "errors.date",
                                            resources
                                                    .getMessage("agreement.label.signedOffDate")));
                }
            }

        }
        return ae;
    }

    public Teesr03Agreement getAgreementForCreate() {
        Teesr03Agreement agreement = new Teesr03Agreement();
        agreement.setEesr03AgreeDescriptionX(this.getDescription());
        agreement.setEesr03AgreeEcolevelDateY(new Timestamp(
                FullEconomicsDateUtil.format(this.getEconLevel())
                        .getTimeInMillis()));
        agreement.setEesr03AgreeEffectiveDateY(new Timestamp(
                FullEconomicsDateUtil.format(this.getEffDate())
                        .getTimeInMillis()));
        agreement.setEesr03AgreeIssueDateY(new Timestamp(FullEconomicsDateUtil
                .format(this.getIssueDate()).getTimeInMillis()));
        if (!StringUtils.isBlank(this.getSignedDate())) {
            agreement.setEesr03AgreeSignatureDateY(new Timestamp(
                    FullEconomicsDateUtil.format(this.getSignedDate())
                            .getTimeInMillis()));
        }
        return agreement;
    }

    public void setAgreementForEdit(Teesr03Agreement agreement) {
        this.setSupplierClass(agreement.getTeess01Supplier()
                .getEess01SupplierClassCodeC());
        this.setSupplierCode(agreement.getTeess01Supplier()
                .getEess01SupplierCodeC());
        Calendar calEconLevel = Calendar.getInstance();
        Calendar calEffDate = Calendar.getInstance();
        Calendar calIssueDate = Calendar.getInstance();
        Calendar calSignDate = Calendar.getInstance();

        calEconLevel.setTimeInMillis(agreement.getEesr03AgreeEcolevelDateY()
                .getTime());
        calEffDate.setTimeInMillis(agreement.getEesr03AgreeEffectiveDateY()
                .getTime());
        calIssueDate.setTimeInMillis(agreement.getEesr03AgreeIssueDateY()
                .getTime());
        calSignDate.setTimeInMillis(agreement.getEesr03AgreeSignatureDateY()
                .getTime());

        this.setEconLevel(FullEconomicsDateUtil.format(calEconLevel));
        this.setEffDate(FullEconomicsDateUtil.format(calEffDate));
        this.setIssueDate(FullEconomicsDateUtil.format(calIssueDate));
        this.setSignedDate(FullEconomicsDateUtil.format(calSignDate));

        this.setDescription(agreement.getEesr03AgreeDescriptionX());
    }

    public Teesr03Agreement getAgreementForUpdate() {
        Teesr03Agreement agreement = new Teesr03Agreement();
        agreement.setTeess01Supplier(this.getAgreementSelected()
                .getTeess01Supplier());
        agreement.setEesr03AgreeEcolevelDateY(new Timestamp(
                FullEconomicsDateUtil.format(this.getEconLevel())
                        .getTimeInMillis()));
        agreement.setEesr03AgreeEffectiveDateY(new Timestamp(
                FullEconomicsDateUtil.format(this.getEffDate())
                        .getTimeInMillis()));
        agreement.setEesr03AgreeIssueDateY(new Timestamp(FullEconomicsDateUtil
                .format(this.getIssueDate()).getTimeInMillis()));
        agreement.setEesr03AgreeSignatureDateY(new Timestamp(
                FullEconomicsDateUtil.format(this.getSignedDate())
                        .getTimeInMillis()));
        agreement.setEesr03AgreeDescriptionX(this.getDescription());
        agreement.setEesr03AgreeCommentsX(this.getAgreementSelected()
                .getEesr03AgreeCommentsX());
        return agreement;
    }

    public void setAgreementMapped(String key, String value) {
        List agreements = (List) this.getAgreementColl();
        if (agreements != null)
            this.setAgreementSelected((Teesr03Agreement) agreements.get(Integer
                    .parseInt(key) - 1));
    }

    public String getBase() {
        return base;
    }

    public void setBase(String base) {
        this.base = base.toUpperCase();
    }

    public String getCommodityCode() {
        return commodityCode;
    }

    public void setCommodityCode(String commodityCode) {
        this.commodityCode = commodityCode;
    }

    public String getDateFrom() {
        return dateFrom;
    }

    public void setDateFrom(String dateFrom) {
        this.dateFrom = dateFrom;
    }

    public String getDateTo() {
        return dateTo;
    }

    public void setDateTo(String dateTo) {
        this.dateTo = dateTo;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix.toUpperCase();
    }

    public Collection<Teesr03Agreement> getAgreementColl() {
        return agreementColl;
    }

    public void setAgreementColl(Collection<Teesr03Agreement> agreementColl) {
        this.agreementColl = agreementColl;
    }

    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix.toUpperCase();
    }

    public String getSupplierCode() {
        return supplierCode;
    }

    public void setSupplierCode(String supplierCode) {
        this.supplierCode = supplierCode.toUpperCase();
    }

    public String getSupplierClass() {
        return supplierClass;
    }

    public void setSupplierClass(String supplierClass) {
        this.supplierClass = supplierClass.toUpperCase();
    }

    public String getSupplierClassFind() {
        return supplierClassFind;
    }

    public void setSupplierClassFind(String supplierClassFind) {
        this.supplierClassFind = supplierClassFind.toUpperCase();
    }

    public String getSupplierCodeFind() {
        return supplierCodeFind;
    }

    public void setSupplierCodeFind(String supplierCodeFind) {
        this.supplierCodeFind = supplierCodeFind.toUpperCase();
    }

    public String getPartClass() {
        return partClass;
    }

    public void setPartClass(String partClass) {
        this.partClass = partClass.toUpperCase();
    }

    public Teesr03Agreement getAgreementSelected() {
        return agreementSelected;
    }

    public Teesr03Agreement getAgreementSelected(String selectedIndex) {
        List agreements = (List) this.getAgreementColl();

        this.setAgreementSelected((Teesr03Agreement) agreements.get(Integer
                .parseInt(selectedIndex) - 1));
        return agreementSelected;
    }

    public void setAgreementSelected(Teesr03Agreement agreementSelected) {
        this.agreementSelected = agreementSelected;
    }

    public String getSelectedIndex() {
        return selectedIndex;
    }

    public void setSelectedIndex(String selectedIndex) {
        this.selectedIndex = selectedIndex;
    }

    public String getUserCode() {
        return userCode;
    }

    public void setUserCode(String userCode) {
        this.userCode = userCode;
    }

    public String getSupplierNameFind() {
        return supplierNameFind;
    }

    public void setSupplierNameFind(String supplierNameFind) {
        this.supplierNameFind = supplierNameFind;
    }

    public String getDateFilter() {
        return dateFilter;
    }

    public void setDateFilter(String dateFilter) {
        this.dateFilter = dateFilter;
    }

    public String getEditBlocked() {
        return editBlocked;
    }

    public void setEditBlocked(String editBlocked) {
        this.editBlocked = editBlocked;
    }

    public String getEconLevel() {
        return econLevel;
    }

    public void setEconLevel(String econLevel) {
        this.econLevel = econLevel;
    }

    public String getSignedDate() {
        return signedDate;
    }

    public void setSignedDate(String signedDate) {
        this.signedDate = signedDate;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getEffDate() {
        return effDate;
    }

    public void setEffDate(String effDate) {
        this.effDate = effDate;
    }

    public String getIssueDate() {
        return issueDate;
    }

    public void setIssueDate(String issueDate) {
        this.issueDate = issueDate;
    }

    public String getVehicleLine() {
        return vehicleLine;
    }

    public void setVehicleLine(String vehicleLine) {
        this.vehicleLine = vehicleLine;
    }
}
